package com.corecloud.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.corecloud.entity.SysDepartmentEntity;
import com.corecloud.entity.SysSchool;
import com.corecloud.service.SysDepartmentService;
import com.corecloud.service.SysSchoolService;
import com.corecloud.service.SysUserRoleService;
import com.corecloud.service.SysUserService;
import com.corecloud.tools.AjaxResultData;
import com.corecloud.tools.PageDemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.security.Principal;
import java.util.List;

/**
 * 地域信息管理相关
 */
@Controller
@RequestMapping("/school")
public class SchoolController {
    @Autowired
    private SysSchoolService sysSchoolService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysDepartmentService departmentService;
    /**
     * 跳转学校管理
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("school/index");
        return modelAndView;
    }

    /**
     * 学校列表页数据（分页）
     * @param principal 登录用户信息
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Page<SysSchool> list(Principal principal, SysSchool info, PageDemo pageDemo){
        QueryWrapper<SysSchool> qw = new QueryWrapper();
        //拼装查询条件
        if(!StringUtils.isEmpty(info.getType())){ qw.eq("type",info.getType()); }
        if(!StringUtils.isEmpty(info.getSchoolName())){ qw.like("school_name",info.getSchoolName()); }
        if(!StringUtils.isEmpty(info.getLayer())){ qw.eq("layer",info.getLayer()); }
        OrderItem oi = new OrderItem();
        oi.setAsc(false);
        oi.setColumn("id");
        pageDemo.addOrder(oi);
        // TODO 获得所有部门
//        List<SysUserRoleEntity> listByParas = sysUserRoleService.getListByParas(new QueryWrapper<SysUserRoleEntity>());
        List<SysDepartmentEntity> listByParas = departmentService.getAll();
        Page<SysSchool> infoByPage = sysSchoolService.getInfoByPage(pageDemo, qw);
        // TODO 将主管部门映射上去
        for (int i = 0; i < infoByPage.getRecords().size(); i++) {
            Long id = infoByPage.getRecords().get(i).getDepartmentId();
            for (int j = 0; j < listByParas.size(); j++) {
                // 获得部门id
                Long departmentId = listByParas.get(j).getDepartmentId();
                // 如果主管部门id和主管部门表的id相同
                if (!StringUtils.isEmpty(id)){
                    if (id.equals(departmentId)){
                        // 得到主管部门名称
                        String name = listByParas.get(j).getName();
                        infoByPage.getRecords().get(i).setDepartmentName(name);
                    }
                }
            }
        }
        return infoByPage;
    }


    /**
     * 添加学校
     * @param principal
     * @return
     */
    @RequestMapping("/add")
    public ModelAndView add(Principal principal){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("school/add");
        return modelAndView;
    }

    /**
     * 批量导入
     * @param principal
     * @return
     */
    @RequestMapping("/upload")
    public ModelAndView upload(Principal principal){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("school/upload");
        return modelAndView;
    }

    @PostMapping("/save")
    @ResponseBody
    public AjaxResultData save(@RequestBody SysSchool sysSchool){
        if (StringUtils.isEmpty(sysSchool.getSchoolName())){
            return new AjaxResultData(301,"学校名称不能为空");
        }
        if (StringUtils.isEmpty(sysSchool.getType())){
            return new AjaxResultData(301,"办学类型不能为空");
        }
        if (StringUtils.isEmpty(sysSchool.getLayer())){
            return new AjaxResultData(301,"办学层次不能为空");
        }
        System.out.println(sysSchool);
        sysSchoolService.save(sysSchool);
        return new AjaxResultData(200,"保存成功");
    }


    @GetMapping("/delete/{id}")
    @ResponseBody
    public AjaxResultData delete(@PathVariable("id") Long id){
        boolean delete = sysSchoolService.delete(id);
        if (delete){
            //删除成功
            return new AjaxResultData(200,"删除成功");
        }else {
            return new AjaxResultData(500,"该学校下有部门挂靠,请确认后删除");
        }

    }

}
